$(function () {
    var isGridOptions = function (o) {//返回参数是否为jgGrid的配置
        return o && typeof o == 'object' && 'colModel' in o;
    };
    if (!$.fn.jqGrid.loadOK) {// 对已经注册过的，不再重复注册
        $.fn.jqGrid = $.aop($.fn.jqGrid, function (o) {
            if (!isGridOptions(o)) return;
            $.fn.jqGrid = $.extend($.fn.jqGrid, {loadOK: true, shrinkToFit:false, forceFit: true});
            var _this = $(this).get(0);
            var hideAndShowCol = function () {
                var colModel = o.colModel;
                var winWidth = $(window).width();
                for (var i in colModel) {
                    var colHideWidth = colModel[i]["hideWidth"];
                    var colShowWidth = colModel[i]["showWidth"];
                    if (colHideWidth) {
                        if (colHideWidth >= winWidth) {
                            $(_this).hideCol(colModel[i]["name"]);
                        } else {
                            $(_this).showCol(colModel[i]["name"]);
                        }
                    }
                    if (colShowWidth) {
                        if (colShowWidth >= winWidth) {
                            $(_this).showCol(colModel[i]["name"]);
                        } else {
                            $(_this).hideCol(colModel[i]["name"]);
                        }
                    }
                }
            };

            var updatePagerIcons = function () {
                var replacement = {
                    'ui-icon-seek-first': 'ace-icon fa fa-angle-double-left bigger-110',
                    'ui-icon-seek-prev': 'ace-icon fa fa-angle-left bigger-110',
                    'ui-icon-seek-next': 'ace-icon fa fa-angle-right bigger-110',
                    'ui-icon-seek-end': 'ace-icon fa fa-angle-double-right bigger-110'
                };
                $('.ui-pg-table:not(.navtable) > tbody > tr > .ui-pg-button > .ui-icon').each(function () {
                    var icon = $(this);
                    var $class = $.trim(icon.attr('class').replace('ui-icon', ''));
                    if ($class in replacement) icon.attr('class', 'ui-icon ' + replacement[$class]);
                })
            };

            var enableTooltips = function () {
                $('.navtable .ui-pg-button').tooltip({container: 'body'});
                $(_this).find('.ui-pg-div').tooltip({container: 'body'});
            };
            o.loadComplete = $.extend(o.loadComplete, {});
            // 只触发一次
            o.loadComplete = $.aop(o.loadComplete, function () {
                // 隐藏或显示列
                hideAndShowCol();
                // 可以使用tooltip
                enableTooltips();
            }, function () {
                // 框架加载完成后，需要改变grid大小
                setTimeout(function () {
                    $(window).triggerHandler('resize.jqGrid');
                    var parent_column = $(_this).closest('[class*="col-"]');
                    $(_this).jqGrid('setGridWidth', parent_column.width());
                    $(_this).closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "auto" });
                }, 20);
                setTimeout(function () {
                    updatePagerIcons();
                }, 10);
            });
            // 窗口绑定事件，设置自适应大小
            $(window).on('resize.jqGrid', function () {
                hideAndShowCol();
            });
        })
    }
});